package com.tepth.maintenancesystem.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.bean.VehicleObj;
import com.tepth.maintenancesystem.adapter.MyDataAdapter;

import java.util.ArrayList;
import java.util.List;

public class SearchStaticListSupportFragment extends Fragment {

    private SuperRecyclerView vehicle_list;
    private MyDataAdapter myAdapter;
    private View rootView;
    private List<VehicleObj> datas;
    private Context context;

    public SearchStaticListSupportFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(rootView==null){
            rootView = inflater.inflate(R.layout.fragment_search_static_list, container, false);
//            context=this.getActivity();
//            addData();
//            initView();
//            initAdapter();
        }

//        ListView listView = (ListView) rootView.findViewById(R.id.search_static_list);
//        ArrayList<String> strings = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            strings.add(i+" -- \n"+getString(R.string.lorem_1));
//        }
//        ListViewAdapter adapter = new ListViewAdapter(getActivity(), strings);
//        listView.setAdapter(adapter);
//        listView.setOnTouchListener(new View.OnTouchListener() {
//            // Setting on Touch Listener for handling the touch inside ScrollView
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                // Disallow the touch request for parent scroll on touch of child view
//                v.getParent().requestDisallowInterceptTouchEvent(true);
//                return false;
//            }
//        });
        return rootView;
    }

    private void initView() {
        vehicle_list = (SuperRecyclerView) rootView.findViewById(R.id.vehicle_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        vehicle_list.setLayoutManager(layoutManager);
        vehicle_list.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        vehicle_list.setLoadMoreEnabled(true);//可以定制是否开启加载更多
        vehicle_list.setLoadingListener(new PullToRefreshListener());//下拉刷新，上拉加载的监听
        vehicle_list.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//下拉刷新的样式
        vehicle_list.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//上拉加载的样式
        vehicle_list.setArrowImageView(R.drawable.arrow_down);//设置下拉箭头
    }

    private void initAdapter() {
        myAdapter = new MyDataAdapter(context,datas);
        vehicle_list.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
//                Intent intent=new Intent(context,VehicleDetailsActivity.class);
//                startActivity(intent);
            }
        });
    }
    private class PullToRefreshListener implements SuperRecyclerView.LoadingListener{

        @Override
        public void onLoadMore() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addData();
                    vehicle_list.completeLoadMore();
                }
            }, 3000);
        }

        @Override
        public void onRefresh() {
//            if(dataList.size() >= 50){
//                superRecyclerView.setNoMore(true);
//                return;
//            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newData();
                    vehicle_list.completeRefresh();
                    myAdapter.notifyDataSetChanged();
                }
            }, 3000);
        }
    }

    /**
     * 添加数据
     */
    private void addData() {
        datas = new ArrayList<>();
//        datas.add(new VehicleObj("81213","混合动力  138","2016/11/28 10:08","1"));
//        datas.add(new VehicleObj("81212","混合动力  138","2016/11/28 10:08","1"));
//        datas.add(new VehicleObj("81211","混合动力  140","2016/11/28 10:08","2"));
//        datas.add(new VehicleObj("81210","混合动力  140","2016/11/28 10:08","3"));
    }

    public void newData() {
        datas.clear();
        addData();
    }
}
